package com.hope;

import java.util.Calendar;
import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

public class DailyPillPagerAdapter extends FragmentStatePagerAdapter {
	private Calendar calendar;
	
    public DailyPillPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    
    public DailyPillPagerAdapter(FragmentManager fm, Calendar cal) {
    	super(fm);
    	calendar = cal;
    }

    @Override
    public Fragment getItem(int i) {
    	calendar.add(Calendar.DAY_OF_MONTH, i);
    	String formattedDate = calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
    	formattedDate += ", ";
    	formattedDate += calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.US);
    	formattedDate += " ";
    	formattedDate += calendar.get(Calendar.DATE);
    	formattedDate += getDateEnding(calendar.get(Calendar.DATE));
    	calendar.add(Calendar.DAY_OF_MONTH, i * -1);
    	
    	DailyPillFragment fragment = new DailyPillFragment();
        Bundle args = new Bundle();
        args.putString(fragment.getArgObject(), formattedDate);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return 100;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "OBJECT " + (position + 1);
    }
    
    private String getDateEnding(int day) {
    	if (day == 1 || day == 21 || day == 31)
    		return "st";
    	else if (day == 2 || day == 22)
    		return "nd";
    	else if (day == 3 || day == 23)
    		return "rd";
    	else
    		return "st";
    }
}
